package com.atguigu.day13.exer;

public class TestSafe2 {
    public static void main(String[] args) {
        Piao p=new Piao();
        Window w1=new Window(p);
        Window w2=new Window(p);
        Window w3=new Window(p);
        w1.start();
        w2.start();
        w3.start();
    }
}
class Window extends Thread{
    private Piao piao;
    public Window(Piao piao){
        this.piao=piao;
    }
    public void run(){
        while (true){
            piao.saleOneTicket();
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class Piao{
    private int total=10;

    public synchronized void saleOneTicket(){
        if (total>0){
            total--;
            System.out.println(Thread.currentThread().getName()+"卖出一张票,剩余:"+total);
        }else {
            System.out.println("票卖完了");
            return;
        }
    }
}